<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-3-9 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		//require_once ROOT_PATH.'include/logic/image.logic.php';
		
		$this->excute();
	}
	
	private function excute()
	{
		switch($this->act)
		{
			case 'list':
				$this->getList();
				break;
			case 'info':
				$this->getInfo();
				break;
			case 'item_list':
				$this->getItemList();
				break;
			case 'item_info':
				$this->getItemInfo();
				break;
			case 'item_comment_list':
				$this->getItemCommentList();
				break;
			case 'add_item_comment':
				$this->addItemComment();
				break;
			case 'up_down':
				$this->doItemUpDown();
				break;
			default:
				$this->message('g_04', STATUS_E);
				break;
		}
		
		$this->showResponse();
	}
	
	private function getList()
	{
		//DBFunction::getInstance()->setDebug();
		
		if(isset($this->request['latitude']) && isset($this->request['longitude']))
		{
			list($lat1, $lng1, $lat2, $lng2) = get_location_range($this->request['latitude'], $this->request['longitude']);
			$this->request['latitude1'] = $lat1;
			$this->request['longitude1'] = $lng1;
			$this->request['latitude2'] = $lat2;
			$this->request['longitude2'] = $lng2;
		}
		
		$result = get_shop_list($this->request);
		
		if(!check_array($result['result'])) $this->message('sh_01', STATUS_F);
		
		foreach($result['result'] as &$temp)
		{
			// get distance from my place
			if(isset($this->request['latitude']) && isset($this->request['longitude']))
			{
				$temp['distance'] = get_distance($this->request['latitude'], $this->request['longitude'], $temp['latitude'], $temp['longitude']);
			}
			
			$temp['desc'] = cut_string($temp['desc'], 140);
			$temp['image'] = ($temp['image'] != '') ? BASE_URL.$temp['image'] : '';
			$temp['image_s'] = str_replace('_o.', '_s.', $temp['image']);
		}
		
		if(isset($this->request['latitude']) && isset($this->request['longitude']))
		{
			usort($result['result'], 'sort_by_dist_asc');
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($result['result']);
		$this->response['list'] = $result['result'];
	}
	
	private function getInfo()
	{
		//DBFunction::getInstance()->setDebug();
		
		$sid = isset($this->request['sid']) ? $this->request['sid'] : '';
		
		if(!$sid)
		{
			$this->message('g_05', STATUS_E);
		}
		
		$result = get_shop_info(array('sid'=>$sid));
		
		if(!isset($result['id']) || $result['id'] < 1)
		{
			$this->message('sh_01', STATUS_E);
		}
		
		$result['image'] = ($result['image'] != '') ? BASE_URL.$result['image'] : '';
		$result['image_s'] = str_replace('_o.', '_s.', $result['image']);
		$result['comment_count'] = get_comment_count(array('id'=>$sid));
		
		$this->response['status'] = STATUS_S;
		$this->response['business_info'] = $result;
	}
	
	private function getItemList()
	{
		//DBFunction::getInstance()->setDebug();
		
		$shop_id = isset($this->request['sid']) ? $this->request['sid'] : '';
		
		if(!$shop_id)
		{
			$this->message('g_05', STATUS_E);
		}
		
		$result = get_shop_item_list($this->request);
		
		if(!check_array($result)) $this->message('sh_02', STATUS_F);
		
		foreach($result as &$temp)
		{
			$temp['image'] = ($temp['image'] != '') ? BASE_URL.$temp['image'] : '';
			$temp['image_s'] = str_replace('_o.', '_s.', $temp['image']);
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($result);
		$this->response['list'] = $result;
	}
	
	private function getItemInfo()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->message('g_05', STATUS_E);
		}
		
		$info = get_shop_item(array('id'=>$id));
		
		if(!isset($info['id']) || $info['id'] < 1) $this->message('sh_02', STATUS_F);
		
		$temp_result = get_shop_item_comment_list(array('id'=>$id));
		$info['comment_count'] = $temp_result['count'];
		
		$info['image'] = ($info['image'] != '') ? BASE_URL.$info['image'] : '';
		$info['image_s'] = str_replace('_o.', '_s.', $info['image']);
		
		$this->response['status'] = STATUS_S;
		$this->response['info'] = $info;
	}
	
	private function getItemCommentList()
	{
		//DBFunction::getInstance()->setDebug();
		
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->message('g_05', STATUS_E);
		}
		
		$info = get_shop_item(array('id'=>$id));
		
		if(!isset($info['id']) || $info['id'] < 1) $this->message('sh_02', STATUS_F);
		
		$result = get_shop_item_comment_list(array('id'=>$id, 'limit'=>$this->request['limit'], 'offset'=>$this->request['offset']));
		$list = $result['result'];
		
		if(!check_array($list)) $this->message('cm_01', STATUS_F);
		
		foreach($list as &$temp)
		{
			$temp['create_date'] = date('Y-m-d', $temp['create_date']);
		}
				
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
	}
	
	private function addItemComment()
	{
		//DBFunction::getInstance()->setDebug();
		
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		$uid = isset($this->request['uid']) ? $this->request['uid'] : '';
		$desc = isset($this->request['desc']) ? $this->request['desc'] : '';
		
		if(!$id)
		{
			$this->message('g_05', STATUS_E);
		}
		
		$info = get_shop_item(array('id'=>$id));
		
		if(!isset($info['id']) || $info['id'] < 1) $this->message('sh_02', STATUS_F);
		
		$result = add_shop_item_comment(array('id'=>$id, 'uid'=>$uid, 'desc'=>$desc));
		
		if($result)
		{
			$this->message('g_06');
		}
		else
		{
			$this->message('g_07', STATUS_F);
		}
	}
	
	private function doItemUpDown()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		$uid = isset($this->request['uid']) ? (int)$this->request['uid'] : '';
		$type = (isset($this->request['type']) &&  $this->request['type'] == 'down') ? 'down' : 'up';
		
		if(!$id)
		{
			$this->message('g_05', STATUS_E);
		}
		
		$visitor = get_visitor(array('id'=>$uid));
		
		if(!isset($visitor['id']) || $visitor['id'] < 1)
		{
			$this->message('g_10', STATUS_E);
		}
		
		$info = get_shop_item(array('id'=>$id));
		if(!isset($info['id']) || $info['id'] < 1) $this->message('sh_02', STATUS_F);
		
		$result = check_up_down_records_exists(array('uid'=>$uid, 'item_id'=>$info['id'], 'type'=>'shop_item'));
		if($result) $this->message('g_07', STATUS_F);
		
		$result = update_shop_item_up_down_count(array('id'=>$id, 'uid'=>$uid, 'type'=>$type));
		
		if($result)
		{
			$this->message('g_06', STATUS_S);
		}
		else
		{
			$this->message('g_07', STATUS_F);
		}
	}
}